package com.bawei.module_mall.http.repository

import com.bawei.lib_net.RetrofitFactory
import com.bawei.module_mall.http.api.ShopApiService
import com.bawei.module_mall.model.AddAddressModel
import com.bawei.module_mall.model.RequestSearchGoodsModel
import com.bawei.module_mall.model.AddShopModel
import com.bawei.module_mall.model.DeleteModel
import com.bawei.module_mall.model.ShopNumberModel
import com.bawei.module_mall.model.UpdateModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlin.jvm.internal.Ref

class ShopRepository {
    val api: ShopApiService by lazy {
        RetrofitFactory.getRetrofitFactory().create(ShopApiService::class.java)
    }

    fun addAddress(addressModel: AddAddressModel) = flow {
        emit(api.postAddAddress(addressModel))
    }.flowOn(Dispatchers.IO)

    fun getAddress() = flow {
        emit(api.getAddress())
    }.flowOn(Dispatchers.IO)

    fun getAddressProvince() = flow {
        emit(api.getAddressProvince())
    }.flowOn(Dispatchers.IO)

    fun getAddressCity(provinceId: String) = flow {
        emit(api.getAddressCity(provinceId))
    }.flowOn(Dispatchers.IO)

    fun getAddressHome(cityId: String) = flow {
        emit(api.getAddressHome(cityId))
    }.flowOn(Dispatchers.IO)

    fun updateAddress(updateModel: UpdateModel) = flow {
        emit(api.updateAddress(updateModel))
    }.flowOn(Dispatchers.IO)

    fun deleteAddress(id: Int) = flow {
        emit(api.deleteAddress(id))
    }.flowOn(Dispatchers.IO)

    fun getShopDetail(id: Int) = flow {
        emit(api.getShopDetail(id))
    }.flowOn(Dispatchers.IO)

    fun addShop(addShopModel: AddShopModel) = flow {
        emit(api.addShop(addShopModel))
    }.flowOn(Dispatchers.IO)

    fun getShop() = flow {
        emit(api.getShop())
    }.flowOn(Dispatchers.IO)

    fun toggleCollect(goodsId: Int) = flow {
        emit(api.toggleCollect(goodsId))
    }.flowOn(Dispatchers.IO)

    fun checkCollect(goodsId: Int) = flow {
        emit(api.checkCollect(goodsId))
    }.flowOn(Dispatchers.IO)

    fun deleteShop(goodsId: DeleteModel) = flow {
        emit(api.deleteShop(goodsId))
    }.flowOn(Dispatchers.IO)

    fun getCollect() = flow {
        emit(api.getCollect())
    }.flowOn(Dispatchers.IO)
    fun getGoodsIds() = flow {
        emit(api.getGoodsIds())
    }.flowOn(Dispatchers.IO)
    /**
     * 查看我的橱窗
     */
    fun viewShowedGoods() = flow {
        emit(api.viewShowedGoods())
    }.flowOn(Dispatchers.IO)

    /**
     * 商品推荐
     */
    fun searchGoods(body: RequestSearchGoodsModel) = flow {
        emit(api.searchGoods(body))
    }

    /**
     * 检查商品是否加入橱窗状态
     */
    fun checkGoods(goodsId: Int) = flow {
        emit(api.checkGoods(goodsId))
    }

    fun toggleGoods(goodsId: Int) = flow {
        emit(api.toggleGoods(goodsId))
    }
    fun getShopNumber() = flow {
        emit(api.getShopNumber())
    }.flowOn(Dispatchers.IO)
    fun updateShopNumber(shopNumberModel: ShopNumberModel) = flow {
        emit(api.updateShopNumber(shopNumberModel))
    }.flowOn(Dispatchers.IO)
    fun getComment(goodsId:Int,pageNum:Int,pageSize: Int) = flow {
        emit(api.getComment(goodsId,pageNum,pageSize))
    }.flowOn(Dispatchers.IO)


    fun queryGoods(body: RequestSearchGoodsModel) = flow {
        emit(api.queryGoods(body))
    }
}